package com.example.checkqufuface.Customer;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.baselibrary.Utils.Customer;
import com.example.baselibrary.Utils.SPUtils;
import com.example.checkqufuface.Base.BaseActivity;
import com.example.checkqufuface.Utils.SelfToastUtil;
import com.example.baselibrary.Utils.StringUtil;
import com.example.checkqufuface.Adapter.WidgetSearchTicketAdapter;
import com.example.checkqufuface.R;
import com.example.checkqufuface.RetrufitHelp.BaseSubscriber;
import com.example.checkqufuface.RetrufitHelp.ExceptionHandle;
import com.example.checkqufuface.RetrufitHelp.RetrofitClient;
import com.example.checkqufuface.bean.QufuQueryOrderListRequsetBean;
import com.example.checkqufuface.bean.QufuQueryOrderListResultBean;

import java.util.List;
import java.util.Map;

public class BottomTicketSearchDialog extends BottomDialogBase implements View.OnClickListener {

    private Context context;

    private ImageView close;
    private Button btSearch;
    private EditText searchNumber;
    private RecyclerView rvTicketDetal;
    private WidgetSearchTicketAdapter searchTicketAdapter;


    public BottomTicketSearchDialog(Context context, final String baseUrl, final Map<String, String> headers, String idCard) {
        super(context);
        this.context = context;


        //进来如果不为空就先查询一次
        if (!StringUtil.isNullOrEmpty(idCard)) {
            // 如果不为空，那么就去查询
            searchNumber.setText(idCard + "");
            BaseActivity.DialogTipInShow("请稍等。。。");
            SearchForData(idCard, baseUrl, headers);
        }

        btSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!StringUtil.isNullOrEmpty(searchNumber.getText().toString())) {
                    // 如果不为空，那么就去查询
                    BaseActivity.DialogTipInShow("请稍等。。。");
                    SearchForData(searchNumber.getText().toString(), baseUrl, headers);
                }else {
                    SelfToastUtil.getInstance().show("请输入查询数据");
                }
            }
        });

        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

    }

    private void SearchForData(String searchDate, final String baseUrl, final Map<String, String> headers) {
        headers.clear();
        String token = (String) SPUtils.get(context, Customer.QUFU_ACCESSTOKEN, "");
        if (!StringUtil.isNullOrEmpty(token)) {
            headers.put("Token", token);
        }
        QufuQueryOrderListRequsetBean orderListRequsetBean = new QufuQueryOrderListRequsetBean();
        orderListRequsetBean.setOrderNo(searchDate);
        RetrofitClient.getInstance(context, baseUrl, headers).queryOrderList(orderListRequsetBean, new BaseSubscriber<QufuQueryOrderListResultBean>(context) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable e) {
                SelfToastUtil.getInstance().show(e.message);
                BaseActivity.DiglogTipInDiss();
            }

            @Override
            public void onNext(QufuQueryOrderListResultBean qufuQueryOrderListResultBean) {
                BaseActivity.DiglogTipInDiss();
                String code = qufuQueryOrderListResultBean.getCode();
                if (code.equals("200")) {

                    List<QufuQueryOrderListResultBean.DataBean> listResultBeanData = qufuQueryOrderListResultBean.getData();

                    if (listResultBeanData.size() <= 0) {
                        SelfToastUtil.getInstance().show("未查询到数据");
                    }

                    searchTicketAdapter = new WidgetSearchTicketAdapter(context, listResultBeanData, baseUrl, headers);
                    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
                    rvTicketDetal.setLayoutManager(linearLayoutManager);
                    rvTicketDetal.setAdapter(searchTicketAdapter);

                } else {
                    SelfToastUtil.getInstance().show(qufuQueryOrderListResultBean.getMessage());
                }
            }
        });
    }

    @Override
    protected void onCreate() {
        setContentView(R.layout.widget_make_ticket_search);

        searchNumber = findViewById(R.id.tv_widget_search_number);

        rvTicketDetal = findViewById(R.id.rv_ticket_search_widget_show);

        close = findViewById(R.id.iv_wg_qu_xiao_ka);
        btSearch = findViewById(R.id.tv_widget_search_button);

    }

    @Override
    public void onClick(View v) {
        dismiss();
    }

    @Override
    public void setOnDismissListener(@Nullable OnDismissListener listener) {
        super.setOnDismissListener(listener);
    }
}


